package blog.util;

import blog.annotations.TableField;
import blog.annotations.TableName;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.LinkedList;
import java.util.List;

public class QueryUtil {

  public static  <T> String[] getFieldName(Class<T> clazz){
    int arrSize = 0;
    List<String> fieldNameList = new LinkedList<>();
    for (Field field : clazz.getFields()) {
      boolean annotationPresent = field.isAnnotationPresent(TableField.class);
      if(annotationPresent){
        boolean value = field.getAnnotation(TableField.class).value();
        if(!value){
          break;
        }else{
          fieldNameList.add(StringUtils.humpToUnderline(field.getName()));
          arrSize++;
        }
      }
      fieldNameList.add(StringUtils.humpToUnderline(field.getName()));
      arrSize++;
    }
    return fieldNameList.toArray(new String[arrSize]);
  }

  /**
   * 获取表名
   */
  public static  <T> String getTableName(Class<T> clazz){
    boolean annotation = clazz.isAnnotation();
    String queryTableName = StringUtils.humpToUnderline(clazz.getSimpleName());
    if(annotation){
      TableName tableName = clazz.getAnnotation(TableName.class);
      String value = tableName.value();
      if(!"".equals(value)){
        queryTableName = value;
      }
    }
    return queryTableName;
  }
}
